<?php


namespace App\Services;


use App\Models\WxUser;
use App\Tools\CurlTools;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\NewsItem;
use Illuminate\Support\Facades\Log;
use EasyWeChat\Kernel\Messages\News;
class MessageService
{
    protected  $app;
    /**
     * WechatService constructor.
     * 实例化Factory
     */
    public function __construct()
    {
        $this->app=Factory::officialAccount(config("wechats"));
    }
    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/23
     * @Time: 21:33
     * @param $arrayData
     * @return mixed|string|void
     * 返回的消息
     */
    public static function typeMessage($arrayData){
        Log::info($arrayData);
        switch ($arrayData['MsgType']){
            case "text":
                return self::TextMessage($arrayData);
            case "image":

            case "voice":
                return self::VoiceMessage();
            case "event":
                self::eventList($arrayData);
                Log::info($arrayData);
                break;
//                    case "videovideo":
//                        return self::TextMessage();
//                    case "shortvideo":
//                        return self::TextMessage();
//                    case "location":
//                        return self::TextMessage();
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/25
     * @Time: 16:47
     * @param $message
     * @return string
     * event事件处理
     */
    public static function eventList($message){
        if($message['Event']=='subscribe'){
            return "欢迎关注";
        }
        #用户openid
        $openId = $message['FormUserName'];
        #查询用户是否存在
        $user = WxUser::where('openid',$openId)->first();
        if(isset($message['EventKey'])){
            if(!$user){
                return  "请先进行账号注册";
            }
        }

    }

    /**
     * 文本
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/9/13
     * @Time: 14:28
     */
    public static function TextMessage($arrayData){
        if($arrayData['Content']=='天气'){
            $count = "有你的天气就很好";
        }elseif ($arrayData['Content']=='图片'){
            Log::info(1);
            $items = [
                new NewsItem([
                    'title'       => "百度的图片",
                    'description' => '...',
                    'url'         => 'https://www.baidu.com/link?url=QrnQqlqrR1hzSFMH9EzwXqvzIzMszZLtvzmwee70ofZVVMGgnSkWF2LeU_AxwAT3bMIjvdnyKehUZO99HNQlMpve2AdRfkYWIqKzdd87nnKIpmuZR8ndVM8DzgjEevCsWHDM9NWfOR2fstEyBmEZJrwmaQyrvZVhRlVNLo1UJhgTqx9x88mRDfYjldrfVuSKuL5TIPswxPhL5PFCJDxrhEwWTa7e1O8PsMMNMDZ8e7XbG5JKxJOQscj9UDOeOP56H_6cfG_99FYedzsi7BFqx4ChuX3a251t5K_9HmdCqERkn6kPOv7MNkYNihKDB26MAS2PcxQnEqr84VWXr5awSEcGbG6KlM55gQ6gA0ACiYe0imZZ_QmIjxn-UpQARgkcvueFAwbNWEUCjY5x1a8nZ-ZW7zCGOf6-cLgAX6OxSbqFhgTD5UPN-0B3sXJ19ZdWhP3pw8OYCA5TWNYxjwDC7A8w6sZONGgDaKNHe6okO5eDHjYSDBr9jnRZMDM3I8tnVFBYc3QZf-44j8RzrfJO3ofy7EKzSIKtFfDsPqHzF0BtaYJjeX3nxk2cGjPmu-mhgM1C6CIIJmLnpkY67qxc0_uF9uBMXzL85mMMEao73Y-UGZtnR2wQyKukhWKv4vEK&wd=&eqid=f6063de70003f0bd000000046389e5ae',
                    'image'       => "",
                ]),
            ];
          //  $news = new News($items);
            Log::info($items);
            return $items;
        }elseif($arrayData['Content']=="模版"){
            $app=Factory::officialAccount(config("wechats"));
         $app->template_message->send([
                'touser' => $arrayData['FromUserName'],
                'template_id' => 'dHgapGqjiX5dRsNNZfeVhTs-pZdZqvLoNbjXsI-MWMg',
                'url' => 'https://easywechat.org',
                'miniprogram' => [
                    'appid' => 'xxxxxxx',
                    'pagepath' => 'pages/xxx',
                ],
                'data' => [
                    'key1' => 'VALUE',
                    'key2' => 'VALUE2',
                ]
            ]);
        }else{
          $url = config("tljqr.tlUrl");
          $data= [
                "reqType" => 0,
                "perception" => [
                    "inputText" => [
                        "text" => $arrayData['Content']
                    ],
                    "inputImage" => [
                        "url" => "imageUrl"
                    ]
                ],
                "userInfo" => [
                    "apiKey" => config("tljqr.tlAppId"),
                    "userId" => $arrayData['FromUserName']
                ]
            ];
            $count = CurlTools::postUrl($url,$data);
            //Log::info($count['results'][0]['values']['text']);
            $count= $count['results'][0]['values']['text'];
        }
            return  $count;

    }

    /**+
     * 图片
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/9/13
     * @Time: 14:27
     */
    public static function ImageMessage(){

    }

    /**
     * 音频
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/9/13
     * @Time: 14:27
     */
    public static function VoiceMessage(){

    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/9/13
     * @Time: 14:41
     * @param $data
     * @param $count
     * @return string
     */
    public static function TextGes($data,$count){
        $newData= '<xml>
                      <ToUserName><![CDATA[%s]]></ToUserName>
                      <FromUserName><![CDATA[%s]]></FromUserName>
                      <CreateTime>%s</CreateTime>
                      <MsgType><![CDATA[%s]]></MsgType>
                      <Content><![CDATA[%s]]></Content>
                    </xml>';
        return sprintf($newData,$data['FromUserName'],$data['ToUserName'],time(),$data['MsgType'],$count);
    }
    public static function ImageGes($data){
        $newData = '<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Image>
                    <MediaId><![CDATA[%s]]></MediaId>
                    </Image>
                    </xml>';
        return sprintf($newData,$data['FromUserName'],$data['ToUserName'],time(),'image','Ni2nG3sibfUpRDh_jtSqBLQgIYd9eCPww83QmwDHBvoxn1ORolA9YGHY6zYawL0M');
    }
}
